<template>
  <page-form :x-uri="uri" :x-tabs="tabs" :bind-rules="rules" :bind-submit="submit" />
</template>

<script>
import PageForm from '@public/template/page_form.vue';
import SparkMD5 from 'spark-md5';
export default {
  components: {
    PageForm
  },
  inject: ['reload'],
  data() {
    return {
      uri: 'admin/user/account/adminer/edit',
      tabs: [
        {
          title: '管理员信息',
          uri: '/user/account/adminer'
        }
      ],
      rules: {
        user_name: [{ required: true, message: '用户名为必填项', trigger: ['blur', 'change'] }],
        password: [
          {
            pattern: /^[a-zA-Z\d\-_&:;\'<>,=%`~!@\\#\(\)\$\^\*\+\|\{\}\[\]\.\?\/]{6,20}$/,
            message: '密码须为数字、字母和英文标点符号组成的6-20位字符',
            trigger: ['blur', 'change']
          }
        ],
        phone: [
          { message: '手机号为必填项', trigger: ['blur', 'change'] },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '请输入正确的手机号',
            trigger: ['blur', 'change']
          }
        ]
      }
    };
  },
  methods: {
    submit: function(form) {
      this.$http.send(
        'PUT',
        '/admin/user/account/adminer/edit',
        {
          ...form,
          password: form.password ? SparkMD5.hash(form.password) : ''
        },
        response => {
          this.$message.success(response.msg, _ => {
            this.reload();
          });
        },
        response => {
          this.disableSubmit = false;
          this.$message.error(response.msg);
        }
      );
    }
  }
};
</script>
